//
//  BindingWaitingViewController.m
//  AlinkDemo
//
//  Created by Vincent on 2017/10/28.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "BindingWaitingViewController.h"
#import <EquesBusiness/YKBusinessFramework.h>
#import "Constant.h"
@interface BindingWaitingViewController ()

@property (nonatomic,copy) NSString *reqid;
@property (nonatomic,assign) NSInteger time;
@property (nonatomic,weak) NSTimer *timer;
@end

@implementation BindingWaitingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.time=0;
    // Do any additional setup after loading the view.
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeout) userInfo:nil repeats:YES];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [self.timer invalidate];
}

-(void)dealloc{
    NSLog(@"销毁");
}

- (void)showSuccess{

    [self performSegueWithIdentifier:@"BindingSuccess" sender:nil];
}
-(void)timeout{
    self.time++;
    if (self.time>120) {
        [self.timer invalidate];
        [SVProgressHUD showErrorWithStatus:@"绑定超时，请检查WiFi密码"];
        [self.navigationController popViewControllerAnimated:YES];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageResult:) name:equesOnMessageResultNotification object:nil];

}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark 收到的消息
- (void)onMessageResult:(NSNotification *)notification{
    NSDictionary *messageResult = [notification object];

    NSString *method = messageResult[@"method"];
    NSInteger code = [messageResult[@"code"] integerValue];
    if ([method isEqualToString:@"on_addbdy_req"]) {
        self.reqid = [messageResult objectForKey:@"reqid"];
        [YKBusinessFramework equesAckAddResponse:self.reqid allow:allowAdd];

    }else if ([method isEqualToString:@"on_addbdy_result"]) {

        switch (code) {
            case 4000:
                [self performSegueWithIdentifier:@"BindingSuccess" sender:nil];
                break;
            case 4002:
                [SVProgressHUD showErrorWithStatus:@" 请求超时, 可能是被请求者长时间未上线, 也可能是被请求者的终端没有回复 "];
                break;
            case 4407:
                [SVProgressHUD showErrorWithStatus:@" 用户和设备之间已经是设备了, 不需要重新添加 "];
                break;
            case 4412:
                [SVProgressHUD showErrorWithStatus:@" name所指定的设备或用户不存在  "];
                break;
            default:
                break;
        }

    }


}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

//    reqid
}

@end
